﻿using BestTrader.Cms.Core.Options;
using BestTrader.Cms.ViewModels;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace BestTrader.Cms.Services
{
    public class GomePayClientService
    {

        protected GomePayOption _gomePayOption;
        public HttpClient Client { get; private set; }


        public GomePayClientService(IOptionsSnapshot<GomePayOption> options, HttpClient httpClient)
        {
            _gomePayOption = options.Get("GOMEPAY");
            httpClient.BaseAddress = new Uri(_gomePayOption.BaseUrl);
            Client = httpClient;
        }


        public async Task<string> AddGomePayDF(GomePayDFModel input)
        {
           
            var url =$"{_gomePayOption.BaseUrl}/api/GomePayDF";
            List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
            paramList.Add(new KeyValuePair<string, string>("amount",input.Amount));
            paramList.Add(new KeyValuePair<string, string>("order_number", input.OrderNumber));
            paramList.Add(new KeyValuePair<string, string>("customer_name", input.CustomerName));
            paramList.Add(new KeyValuePair<string, string>("account_number", input.AccountNumber));
            paramList.Add(new KeyValuePair<string, string>("issue_bank_name", input.IssueBankName));
            paramList.Add(new KeyValuePair<string, string>("pay_password", input.PayPassword));
            paramList.Add(new KeyValuePair<string, string>("memo", "用户出金"));

            //url = "http://localhost:50013/api/GomePayDF?order_number=TEST100000000000002&customer_name=zzc&amount=0.01&account_number=6217850800012786339&issue_bank_name=中国银行&memo=11111&pay_password=5555555";

            var response = await Client.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList));

            return await response.Content.ReadAsStringAsync();

        }

        public async Task<string> GetGomePayDFBillByOrderNumber(string orderNumber)
        {

            var url = $"{_gomePayOption.BaseUrl}/api/GomePayDF";
            //url = "http://localhost:50013/api/GomePayDF?order_number=TEST100000000000002&customer_name=zzc&amount=0.01&account_number=6217850800012786339&issue_bank_name=中国银行&memo=11111&pay_password=5555555";
            //orderNumber = "TEST100000000000002";

            url = $"{url}?orderNumber={orderNumber}";
            var response = await Client.GetAsync(new Uri(url));

            return await response.Content.ReadAsStringAsync();

        }

        
    }

}